package ForkJoin框架.RecursiveAction;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveAction;

public class Repeat extends RecursiveAction {
    int threshhold = 10;
    static  int index = 0;
    int target;

    public Repeat(int target) {
        this.target = target;
    }

    @Override
    protected void compute() {
        if (target <= threshhold)
            for (int i = 0;i < target;i++)
                index++;
        else{
            for (int i = 1;i <= target / 10;i++){
                Repeat repeat = new Repeat(10);
                repeat.fork();
//                repeat.join(); // 不写也行
            }
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        Repeat repeat = new Repeat(50);
        ForkJoinTask<Void> submit = forkJoinPool.submit(repeat);
        submit.get(); // 启动。不写相当于没有执行该功能
        System.out.println(repeat.index);// 50
    }
}
